package com.yuwenchao.superafflatus.controller;


import com.yuwenchao.superafflatus.service.impl.QRCodeService;
import com.yuwenchao.superafflatus.utils.R;
import com.yuwenchao.superafflatus.utils.RandomUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "二维码信息")
@RequestMapping("/ewmDemo")
@RestController
@Slf4j
public class QRCodeController {

    @Autowired
    private QRCodeService qrCodeService;



    @ApiOperation("二维码展示")
    @GetMapping("qrCode")
    public R getQRCode(@ApiParam(value = "二维码URL") @RequestParam(value="codeContent" ,defaultValue = "https://u.wechat.com/MDS3XLQCIDu493vfucWRqeo")String codeContent, HttpServletResponse response) {
        //codeContent="https://www.baidu.com/";  //测试给百度生成一个二维码
        //codeContent="https://u.wechat.com/MDS3XLQCIDu493vfucWRqeo";
        String random = RandomUtils.getFourBitRandom();

     //   String filePath ="E:\\temp\\myWatch.png";   //如果是不熟在linux上要修改路劲

        String filePath ="/root/temp/myWatch"+random+".png";


        try {
           // qrCodeService.createQRCode2Stream(codeContent, response);   //生成到流
            qrCodeService.createQRCode2File(codeContent,filePath);      //生成到文件
            log.info("成功生成二维码！");
        } catch (Exception e) {
            log.error("发生错误， 错误信息是：{}！", e.getMessage());
        }
        return R.ok().message("生成文件："+filePath);
    }

}